package com.bluetrum.abmate.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetrum.abmate.databinding.CardPodDeviceBinding;
import com.bluetrum.devicemanager.db.BaseDevice;

import java.util.List;

public class BaseDeviceAdapter extends RecyclerView.Adapter<BaseDeviceAdapter.ViewHolder>
        implements View.OnLongClickListener {

    private final LiveData<List<BaseDevice>> mBaseDevices;

    private OnItemLongClickListener mOnItemLongClickListener;

    public BaseDeviceAdapter(Fragment fragment, LiveData<List<BaseDevice>> baseDevices) {
        this.mBaseDevices = baseDevices;
        baseDevices.observe(fragment.getViewLifecycleOwner(), devices -> notifyDataSetChanged());
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.mOnItemLongClickListener = onItemLongClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        CardPodDeviceBinding binding = CardPodDeviceBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // TODO: 填充内容
        holder.contentView.setBackgroundColor(Color.CYAN);

        //noinspection ConstantConditions
        BaseDevice baseDevice = mBaseDevices.getValue().get(position);

        holder.deviceNameTextView.setText(baseDevice.getName());
        holder.deviceIdentifierTextView.setText(baseDevice.getAddress());

        holder.contentView.setTag(baseDevice);
        holder.contentView.setOnLongClickListener(this);
    }

    @Override
    public int getItemCount() {
        return mBaseDevices.getValue() != null ? mBaseDevices.getValue().size() : 0;
    }

    @Override
    public boolean onLongClick(View v) {
        BaseDevice baseDevice = (BaseDevice) v.getTag();
        if (mOnItemLongClickListener != null) {
            mOnItemLongClickListener.OnItemLongClick(baseDevice);
        }
        return true;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView deviceNameTextView;
        TextView deviceIdentifierTextView;
        ImageView deviceImageView;
        TextView onlineStatusTextView;

        public ViewHolder(final CardPodDeviceBinding binding) {
            super(binding.getRoot());
            contentView = binding.contentView;
            deviceNameTextView = binding.deviceNameTextView;
            deviceIdentifierTextView = binding.deviceIdentifierTextView;
            deviceImageView = binding.deviceImageView;
            onlineStatusTextView = binding.onlineStatusTextView;
        }
    }

    public interface OnItemLongClickListener {
        void OnItemLongClick(BaseDevice baseDevice);
    }

}
